﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_KhachHang_Customer_Info : System.Web.UI.UserControl
{
    TblCustomerManager _TblCustomerManager = new TblCustomerManager();
    public int CusID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { CusID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CusID = 0; }

            FunctionExtend.LoadCustomerStatus(ddlStatus) ;
            FunctionExtend.LoadGender(ddlGender);

            if (CusID == 0) //Add cate
            {
                imgbtn_Delete.Enabled = false;
                img_news.Visible = imgbtn_Delete.Visible = false;
                lblTitle.Text = "Thêm khách hàng";
                Toolbox1.ShowDel = Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa thông tin khách hàng: " + CusID;
                BindData(CusID);
            }
        }
    }


    //load chi tiet san pham
    protected void BindData(int ID)
    {
        TblCustomerEntity _TblCustomerEntity = _TblCustomerManager.SelectOne(ID);
        if (_TblCustomerEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtUserName.Text = Server.HtmlDecode(_TblCustomerEntity.UserName);
        txtFullname.Text = Server.HtmlDecode(_TblCustomerEntity.FullName);
        txtEmail.Text = Server.HtmlDecode(_TblCustomerEntity.Email);
        txtBirthDay.Text = _TblCustomerEntity.Birthday.ToString();
        ddlGender.SelectedValue = _TblCustomerEntity.Gender.ToString();
        txtAddress.Text = Server.HtmlDecode(_TblCustomerEntity.Address);
        txtPhone.Text = Server.HtmlDecode(_TblCustomerEntity.Telephone);
        ddlStatus.SelectedValue = _TblCustomerEntity.Status;
        fckMoTa.Value = Server.HtmlDecode(_TblCustomerEntity.Mota);
        img_news.Visible = true;
        img_news.ImageUrl = (_TblCustomerEntity.LoGo != "") ? Server.HtmlDecode(_TblCustomerEntity.LoGo) : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblCustomerEntity.LoGo))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblCustomerManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tên tài khoản !</span>"; txtUserName.Focus(); return; }
        if (txtEmail.Text =="") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập Email !</span>"; txtEmail.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/CustomerEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblCustomerEntity _TblCustomerEntity = new TblCustomerEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblCustomerEntity.Id = Convert.ToInt32(hidID.Value);

        //_TblCustomerEntity.TruyCap = 1;
        _TblCustomerEntity.UserName = Server.HtmlEncode(txtUserName.Text);
        _TblCustomerEntity.Pass = HNG.LIB.Localization.LocalizationUtils.MaHoaMatKhau("123456");
        _TblCustomerEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblCustomerEntity.FullName = Server.HtmlEncode(txtFullname.Text);
        _TblCustomerEntity.Email = Server.HtmlEncode(txtEmail.Text);
        _TblCustomerEntity.Address = Server.HtmlEncode(txtAddress.Text);
        _TblCustomerEntity.Telephone = Server.HtmlEncode(txtPhone.Text);
        _TblCustomerEntity.Mota = Server.HtmlEncode(fckMoTa.Value);
        if (!string.IsNullOrEmpty(txtBirthDay.Text))
            _TblCustomerEntity.Birthday = Convert.ToDateTime(txtBirthDay.Text);
        _TblCustomerEntity.Gender = ddlGender.SelectedValue;
        _TblCustomerEntity.Status = ddlStatus.SelectedValue;
        if (FileUpload1.FilePath != "")
        {
            string ImageDefault = Server.MapPath(_TblCustomerEntity.LoGo);
            try { File.Delete(ImageDefault); }
            catch { }
            _TblCustomerEntity.LoGo = Server.HtmlEncode(FileUpload1.FilePath);
        }
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblCustomerManager.Update(_TblCustomerEntity);
        }
        else
        {
            _TblCustomerManager.Insert(_TblCustomerEntity);
            hidID.Value = _TblCustomerEntity.Id.ToString();
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblCustomerEntity _TblCustomerEntity = _TblCustomerManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblCustomerEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblCustomerEntity.LoGo))
                {
                    string defaultImage = Server.MapPath(_TblCustomerEntity.LoGo);
                    try { File.Delete(defaultImage); }
                    catch { }
                    _TblCustomerEntity.LoGo = "";
                    _TblCustomerManager.Update(_TblCustomerEntity);
                }
            }
        }
        BindData(Convert.ToInt32(hidID.Value));
    }
}